// #packager-optimize

(function() {

    var $ = juliet.jQuery;

    var modalWindowModule = {

        background : null,

        window : null,

        content : null,

        contentParent : null,

        initialized : false,

        bindCloseLinkOnClickHandler : function() {
            var self = this;

            this.window.find('.window-header .close').click(function() {
                self.hide();
                return false;
            });
        },

        bindEventsHandlers : function() {
            this.bindCloseLinkOnClickHandler();
        },

        initialize : function() {
            this.background = $('.modal-window-background');
            this.window = $('.modal-window-wrapper');

            this.bindEventsHandlers();

            this.initialized = true;
        },

        setContent : function(selector) {
            this.content = $(selector);
            this.contentParent = this.content.parent();

            this.content.show();
            this.window.find('.window-content').append(this.content);
        },

        removeContent : function() {
            this.content.hide();
            this.contentParent.append(this.content);

            this.content = null;
            this.contentParent = null;
        },

        setTitle : function(title) {
            this.window.find('.window-header .window-title').html(title);
        },

        setWidth : function(width) {
            this.window.find('.modal-window').css('width', width);
        },

        show : function(o) {
            if (!this.initialized) this.initialize();

            this.setTitle(o.title);
            this.setContent(o.contentSelector);
            this.setWidth(o.width);

            this.background.show();
            this.window.show();
        },

        hide : function() {
            this.background.hide();
            this.window.hide();
            this.removeContent();
        }

    };

    juliet.registerModule('juliet.ui', 'modalWindow-0.1', modalWindowModule);

})();
